// pages/ruku/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    bar_code:'',
    id:'',
    num:'',
    type:'',
    materialId:'',
    warehouseText:'',
    warehouseValue:[],
    wareHouseId:'',
    wareHouseData:[],
    wText:'',
    wValue:[],
    wId:'',
    loctarText:'',
    loctarValue:[],
    loctarId:'',
    loctarData:[],
    ywText:'',
    ywValue:[],
    ywId:'',
    ywData:[],
    inMdId:'',
    isSub:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
this.getWareHouse();
this.getYwData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  burD(e){
    this.setData({bar_code:e.detail.value,materialId:'',materialText:''})
    this.getInfo();
  },
  warehousePicker(){
    this.setData({ warehouseVisible: true});
  },
  wPicker(){
    this.setData({ wVisible: true});
  },
  loctarPicker(){
    this.setData({  loctarVisible: true});
  },
  ywPicker(){
    this.setData({ywVisible:true})
  },
  selectWareHouse(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
  },
  onPickerCancel(e){
    const { key } = e.currentTarget.dataset.key;
  
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  bindKeyInput: function (e) {
    this.setData({materialText:e.detail.value,bar_code:''})
    let that=this;
    let param={mdName:e.detail.value};
    if(e.detail.value==''){
      this.setData({mdId:'',materialVisible:false})
      return;
    }
    app.common.http_post('/miniProgram/Other/getMaterial',param,function(res){
      let data=res.data;
      console.log('wlData',data);
      that.setData({materialData:data})
      if(data.length>0&&e.detail.value!=''){
        that.setData({materialVisible:true})
      }
        
     
    })
    

  },
  selectMaterial(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
    this.getInfo();
  },
  getWareHouse(){
    let that=this;
    app.common.http_post('/miniProgram/Other/getWarehouse',{},function(res){
          let data=res.data;
         that.setData({wareHouseData:data});
        })
  },
  getYwData(){
    let that=this;
    app.common.http_post('/miniProgram/Other/getywtype',{},function(res){
      let data=res.data;
     that.setData({ywData:data});
    })
  },
  getInfo(){
    let that=this;
    that.setData({detail:{}})
    let bar_code=that.data.bar_code;
    let materialId=that.data.materialId;
  
    if(bar_code==''&&materialId==''){
      return;
    }
    let param={bar_code:bar_code,mdId:materialId};
    app.common.http_post('/miniProgram/other/getMaterialInfo',param,function(res){
  console.log('res',res)
      let data=res.data;
      console.log('wlData',data);

     that.setData({detail:data,inMdId:data.id});
    })
  },
  scanCode(){
    let that=this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
    console.log('scr',res)
        if (res.errMsg === "scanCode:ok") {
          let code=res.result;
        that.setData({bar_code:code,materialId:'',materialText:''})
        that.getInfo()
        }
      }
    })
  },
  setNum(e){
  
    this.setData({num:e.detail.value})
  },
  doSure(){
    let that=this;
    let wId=that.data.warehouseId;
    let innum=that.data.num;
    let type=that.data.ywText;
    let mdId=that.data.inMdId;
    console.log('data',that.data)
    if(wId==''||innum==''||type==''||mdId==''){
      app.common.alert('参数缺失');
      return false;
    }
  
    let param={
      mdId:mdId,
      num:innum,
      type:type,
      wId:wId
    };
    app.common.showLoading();
    app.common.http_post('/miniProgram/InReceipt/onSure',param,function(res){
      app.common.alert(res.msg)
      app.common.hideLoading()
      setTimeout(function(){
        app.common.go_back()
      },500)
        })
  }
})